package de.fhbrs.migration;

import java.io.*;
import java.rmi.RemoteException;
import fedora.server.types.gen.MIMETypedStream;
import fedora.client.FedoraClient;
import fedora.server.management.FedoraAPIM;
import fedora.server.access.FedoraAPIA;


/**
 * Generates a LitsearchFedoraConnection
 * This class is implementing the Fedora APIA and APIM
 * 
 * @version $Id: LitsearchFedoraConnection.java 379 2008-01-17 15:27:36Z andreas.moser.bm $
 * @author 	Andreas Moser, Stephan Schmitt
 * 
 * 001 10.12.2007 - Andreas Moser - Added all Standard Comments
 * 
 */

public class LitsearchFedoraConnection {
	private FedoraAPIM APIM;
	private FedoraAPIA APIA;
	private FedoraClient Fedora;
    
    /**
     * Constructor will create a connection to Fedora
     * @throws Exception
     */
    public LitsearchFedoraConnection(String host, int port, String user, String pass, String protocol) throws Exception {
    	// Use the FedoraClient utility to get SOAP stubs.
        // These SOAP stubs enable the client to connect to a Fedora repository
        // via the API-A and API-M web service interfaces.

        String baseURL = protocol + "://" + host + ":" + port + "/fedora";
        Fedora = new FedoraClient(baseURL, user, pass);
        APIM=Fedora.getAPIM();
        APIA=Fedora.getAPIA();
    }
    
    /**
     * With this it is possible to get for a given pid 
     * the Dublin Core from Fedora.
     * If there exists no pid in Fedora, getDC will throw an Exception.
     *  
     * @param String pid
     * @return byte[]
     * @throws RemoteException
     */
    public byte[] getDC(String pid) throws RemoteException {
    	MIMETypedStream ds = APIA.getDatastreamDissemination(pid,"DC",null);
		return ds.getStream();
    }
    
    /**
     * This function creates a new digital Object in Fedora.
     * The information how the digital Object looks like, 
     * comes from the InputStream. 
     * The function returns the new PID.
     * 
     * @param InputStream DC
     * @return String pid
     * @throws RemoteException
     * @throws IOException
     */
    public String insertFedoraObject(InputStream DC)throws RemoteException, IOException
    {
    	ByteArrayOutputStream out=new ByteArrayOutputStream();
    	pipeStream(DC, out, 4096);
    	String pid = APIM.ingest(out.toByteArray(), "foxml1.0", "insert");
    	return pid;
    }
    
    /**
     * @param InputStream in
     * @param OutputStream out
     * @param int bufSize
     * @throws IOException
     */
    private void pipeStream(InputStream in, OutputStream out, int bufSize)
	throws IOException {
    	try {
    		byte[] buf = new byte[bufSize];
    		int len;
    		while ( ( len = in.read( buf ) ) > 0 ) {
    			out.write( buf, 0, len );
    		}
    	} finally {
    		try {
    			in.close();
    			out.close();
    		} catch (IOException e) {
    			System.err.println("WARNING: Could not close stream.");
    		}
    	}
    }   
}
